package com.guanyong.admin.controllers.api;

import com.guanyong.admin.handle.UploadFileType;
import com.guanyong.model.SubjectModel;
import com.guanyong.services.MessageService;
import com.guanyong.services.SubjectService;
import com.guanyong.utils.Comm.ResultObject;
import com.guanyong.utils.NetUtils.UploadFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * Created by mxk on 2017/9/22.
 */
@RestController
@RequestMapping("/api/subject/")
public class SubjectApi {
    @Autowired
    SubjectService subjectService;

    @Autowired
    UploadFileUtil uploadFileUtil;

    @RequestMapping(value = "subjectalllist", method = RequestMethod.GET)
    ResultObject<List<SubjectModel>> getsubjectalllist(HttpServletRequest request ) {
        ResultObject<List<SubjectModel>> result = new ResultObject<List<SubjectModel>>();
        List<SubjectModel> list=subjectService.GetSubjectAllModel();
        result.setResult(list);
        return result;
    }

    @RequestMapping(value = "addsubject", method = RequestMethod.POST)
    ResultObject<Integer> addSubject(HttpServletRequest request ,
                                     @RequestParam String  subject_name,
                                     @RequestParam String subject_desc,
                                     @RequestParam Integer subject_type,
                                     @RequestParam(value = "film_pic",required = false) MultipartFile
                                             subject_pic) {
        SubjectModel subjectModel=new SubjectModel();
        subjectModel.setfDesc(subject_desc);
        subjectModel.setfTitle(subject_name);
        String path  ="";
        try {
            path = uploadFileUtil.SaveFileByMultiFlie(subject_pic,request, UploadFileType.subject);
            //path = uploadFileUtil.SaveStreamFile(UploadFileType.Photo, ".jpg", request);
        } catch (Exception e) {
            e.printStackTrace();
            path="";
        }
        subjectModel.setfType(subject_type);
        subjectModel.setfPhoto(path);
        return subjectService.AddSubject(subjectModel);
    }

    @RequestMapping(value = "videosubjectlist", method = RequestMethod.GET)
    ResultObject<List<SubjectModel>> getvideosubjectlist(HttpServletRequest request,
                                                         @RequestParam String videoid) {
        ResultObject<List<SubjectModel>> result = new ResultObject<List<SubjectModel>>();
        List<SubjectModel> list=subjectService.selectViodeSubjectList(videoid);
        result.setResult(list);
        return result;
    }

    @RequestMapping(value = "subjectvideoalllist", method = RequestMethod.GET)
    ResultObject<List<SubjectModel>> subjectVideoalllist(HttpServletRequest request ,
                                                         @RequestParam String videoid) {
        ResultObject<List<SubjectModel>> result = new ResultObject<List<SubjectModel>>();
        List<SubjectModel> list=subjectService.GetSubjectVideoAllModel(videoid);
        result.setResult(list);
        return result;
    }


    @RequestMapping(value = "delsubjectvideo", method = RequestMethod.POST)
    ResultObject<Integer> DelSubjectVideo(HttpServletRequest request ,
                                        @RequestParam Integer subject_id,
                                          @RequestParam String videoid ) {

        return subjectService.DeleteSubjectVideo(subject_id,videoid);
    }

    @RequestMapping(value = "updatesubjectvideo", method = RequestMethod.POST)
    ResultObject<Integer> UpdateSubjectVideo(HttpServletRequest request ,
                                        @RequestParam String videoid,
                                        @RequestParam String[]  subjectId
                                        ) {

        return subjectService.updatesubjectvideo(videoid,subjectId);
    }


    @RequestMapping(value = "updatesubject", method = RequestMethod.POST)
    ResultObject<Integer> UpdateSubject(HttpServletRequest request ,
                                     @RequestParam Integer subject_id,
                                     @RequestParam String  subject_name,
                                     @RequestParam String subject_desc,
                                     @RequestParam Integer subject_type,
                                     @RequestParam(value = "film_pic",required = false) MultipartFile
                                             subject_pic) {
        SubjectModel subjectModel=new SubjectModel();
        subjectModel.setfDesc(subject_desc);
        subjectModel.setfTitle(subject_name);
        String path  ="";
        try {
            path = uploadFileUtil.SaveFileByMultiFlie(subject_pic,request, UploadFileType.subject);
            //path = uploadFileUtil.SaveStreamFile(UploadFileType.Photo, ".jpg", request);
        } catch (Exception e) {
            e.printStackTrace();
            path="";
        }
        subjectModel.setfType(subject_type);
        subjectModel.setfId(subject_id);
        subjectModel.setfPhoto(path);
        return subjectService.UpdateSubject(subjectModel);
    }




    @RequestMapping(value = "deletesubject", method = RequestMethod.POST)
    ResultObject<Integer> DeleteSubject(HttpServletRequest request ,
                                        @RequestParam Integer subject_id) {

        return subjectService.DeleteSubject(subject_id);
    }
}
